<template>
  <div>
    <div v-show="!(isShowAnnouncement || isShowSearchNotice)">
      <jxf-titletip :title="title" @refreshPage="refreshFuc"></jxf-titletip>
      <el-card class="mt20">
        <el-form :inline="true" :model="searchInfo" ref="searchInfo">
          <el-form-item label="消息标题:" prop="name">
            <el-input v-model.trim="searchInfo.name" clearable @keyup.enter.native="enterNextInput('creatername')" ref="input-name"></el-input>
          </el-form-item>
          <el-form-item label="展示对象:" prop="terminal">
            <el-select v-model="searchInfo.terminal" clearable placeholder="全部">
              <el-option label="全部" value="0"></el-option>
              <el-option label="买家端" value="1"></el-option>
              <el-option label="卖家端" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发布人员:" prop="creatername">
            <el-input v-model.trim="searchInfo.creatername" clearable @keyup.enter.native="onSearch" ref="input-creatername"></el-input>
          </el-form-item>
          <el-form-item label="发布时间:" prop="createTime">
            <el-date-picker
              v-model="searchInfo.createTime"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch()">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="resetForm('searchInfo')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="jxf-table-card mt20">
        <div slot="header" class="clearfix">
          <span>数据列表</span>
          <div>
            <el-button type="primary" @click="postMsg">发布消息</el-button>
            <el-button type="primary" @click="exportExcel" :loading="isSuccess">导出</el-button>
          </div>
        </div>
        <jxf-table class="" :table="dataTable">
          <template slot="time" slot-scope="notice">
            <div>开始时间：{{notice.row.startTime}}</div>
            <div>结束时间：{{notice.row.endTime}}</div>
          </template>
          <template slot="terminal" slot-scope="notice">
            <div v-if="notice.row.terminal == '0'">全部</div>
            <div v-if="notice.row.terminal == '1'">买家端</div>
            <div v-if="notice.row.terminal == '2'">卖家端</div>
          </template>
          <template slot="operation" slot-scope="notice">
            <el-button type="text" @click="watchMsg(notice.row)">查看</el-button>
            <el-button type="text" @click="EditMsg(notice.row)">编辑</el-button>
            <el-button type="text" @click="deleteItem(notice.row)">删除</el-button>
          </template>
        </jxf-table>
      </el-card>
      <jxf-pagination class="mt20" :pageTotal="pageTotal" :currentPage.sync='currentPage' :pageSize.sync="pageSize"
                      @currentPageChange="currentPageChange" @pageSizeChange="pageSizeChange"></jxf-pagination>
    </div>
    <Announcement v-if="isShowAnnouncement" @fresh="fresh" @goBack="goBack" :dataInfo="dataInfo" :title="detailTitle"></Announcement>
    <SearchNotice :isShowDetail.sync="isShowDetail" :dataInfo="dataInfo"></SearchNotice>
  </div>
</template>

<script>
  import Announcement from './components/Announcement'
  import SearchNotice from './components/SearchNotice'
  import FormatDate from '@/assets/js/formatDate'
    export default {
        name: "NoticeNews",
        components: { Announcement, SearchNotice },
        data() {
          return {
            title: '公告消息',
            searchInfo: {
              name: "",
              terminal: "",
              createTime: "",
              creatername: ""
            },
            isSuccess: false,
            dataInfo: {},
            isShowAnnouncement: false,
            isShowSearchNotice: false,
            FormatDate: FormatDate,
            isShowDetail: false,
            dataTable: {
              border:true,
              loading:false,
              hasSelect:false,
              hasOperation:false,
              tr: [
                {
                  id: '2',
                  label: '公告标题',
                  prop: 'name'
                },
                {
                  id: '3',
                  label: '展示对象',
                  show: 'template',
                  prop: 'terminal'
                },
                {
                  id: '4',
                  label: '发布时间',
                  width: "160",
                  prop: 'createTime'
                },
                {
                  id: '5',
                  width: '240',
                  label: '发布时间段',
                  show: 'template',
                  prop: 'time'
                },
                {
                  id: '6',
                  label: '发布人员',
                  prop: 'creatername'
                },
                {
                  id: '7',
                  label: '操作',
                  show: 'template',
                  width: "140",
                  fixed: "right",
                  prop: 'operation'
                }
              ],
              operation: [],
              data: []
            },
            pageTotal:0,
            pageSize:10,
            currentPage:2,
            // 发布公告-编辑公告标题
            detailTitle: ''
          }
        },
        methods: {
          fresh() {
            this.getData()
          },

          goBack() {
            this.dataInfo = {};
            this.isShowAnnouncement = false
          },

          getData() {
            this.dataTable.loading = true;
            let data = {
              ...this.searchInfo,
              pageNum: this.currentPage,
              pageSize: this.pageSize
            };
            ajax.post(apis.operate.getNoticeList, data).then(res => {
              if(res.code === '10000000') {
                this.dataTable.data = res.data.dataList;
                this.pageTotal = res.data.total
              } else {
                this.$message.error(res.message)
              }
              this.dataTable.loading = false
            })
          },

          refreshFuc() {
            this.resetForm('searchInfo');
            this.pageSize = 10;
            this.currentPage = 1;
            this.getData()
          },

          resetForm(formName) {
            this.$refs[formName].resetFields();
          },

          onSearch() {
            this.currentPage = 1;
            this.getData();
          },

          // 切换页面
          currentPageChange(pageNo) {
            this.currentPage = pageNo;
            this.getData()
          },

          enterNextInput(inputName) {
            this.$refs["input-" + inputName].focus();
          },

          // 切换页面条数
          pageSizeChange(pageNo) {
            this.pageSize = pageNo;
            this.getData()
          },

          // 发布消息
          postMsg() {
            this.detailTitle = '发布公告';
            this.isShowAnnouncement = true;
          },

          // 查看公告
          watchMsg(obj) {
            this.dataInfo = obj;
            this.isShowDetail = true
          },

          // 编辑
          EditMsg(obj) {
            this.detailTitle = '编辑公告';
            this.dataInfo = obj;
            this.dataInfo = {
              ...this.dataInfo,
              isShow: this.dataInfo && this.dataInfo.isShow == '1' ? true : false,
              terminal: this.dataInfo.terminal.toString()
            };
            this.isShowAnnouncement = true
          },

          // 删除
          deleteItem(obj) {
            // 删除;
            let data = {
              id: obj.id,
              isdel: 1
            };
            this.$confirm('此操作将删除此公告, 是否继续?', '删除公告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              ajax.post(apis.operate.operateNotice, data).then(res => {
                if(res.code === '10000000') {
                  this.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
                  this.getData()
                } else {
                  this.$message.error(res.message)
                }
              })
            }).catch(() => {
            });
          },

          // 导出
          exportExcel() {
            this.isSuccess = true;
            let data = {
              ...this.searchInfo,
              pageNum: this.currentPage,
              pageSize: this.pageSize
            };
            ajax.export(apis.operate.exportNotice, data).then((res) => {
              this.isSuccess = false;
              if(res.code) {
                if(res.code === "10000000") {

                } else {
                  this.$message.error(res.message)
                }
              }
            })
          },
        },
        created() {
          this.onSearch()
        }
    }
</script>

<style lang="scss" scoped>

</style>
